package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sola/article"
	"sola/category"
	"sola/classify"
	"sola/image"
	"sola/panel"
	"sola/router/middleware"
	"sola/tag"
	"sola/user"
)

func InitRouter(r *gin.Engine) {
	initPlugins(r)
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})
	initV1Group(r)
}

func initPlugins(r *gin.Engine) {
	r.Use(middleware.CorsMiddleware())
	r.Use(middleware.PrintHeaders())
}

func initV1Group(r *gin.Engine) {
	v1 := r.Group("/v1")
	{
		search := v1.Group("/search")
		{
			search.GET("/user", user.SearchUser)
		}
		images := v1.Group("/images")
		{
			images.GET("/", image.ListImages)
			images.GET("/:iamge_id", image.GetImage)
			//images.POST("/", middleware.AuthMiddleware, image.UploadFile)
			images.POST("/", image.UploadFile)
		}
		users := v1.Group("/user")
		{
			users.POST("/login", user.Login)
			users.POST("/regist", user.Regist)
			users.POST("/logout", user.Logout)
			users.GET("/info", user.GetUserInfo)
		}
		v1BaseUser := v1.Group("/api/:user_id")
		articles := v1BaseUser.Group("/articles")
		{
			articles.GET("", article.List)
			articles.POST("", middleware.AuthMiddleware, article.Create)
			articles.PUT("/:article_id", middleware.AuthMiddleware, article.Update)
			articles.GET("/:article_id", article.Detail)
			articles.DELETE("/:article_id", middleware.AuthMiddleware, article.Delete)
		}
		classifies := v1BaseUser.Group("/classifies")
		{
			classifies.GET("", classify.List)
			classifies.POST("", classify.Create)
			classifies.PUT("", classify.Update)
			classifies.GET("/:classify_id", classify.Detail)
			classifies.DELETE("/:classify_id", classify.Delete)
		}
		categories := v1BaseUser.Group("/categories")
		{
			categories.GET("", category.List)
			categories.POST("", middleware.AuthMiddleware, category.Create)
			categories.PUT("/:category_id", middleware.AuthMiddleware, category.Update)
			categories.GET("/:category_id", category.Detail)
			categories.DELETE("/:category_id", middleware.AuthMiddleware, category.Delete)
		}
		tags := v1BaseUser.Group("/tags")
		{
			tags.GET("", tag.List)
			tags.POST("", tag.Create)
			tags.PUT("/:tag_id", tag.Update)
			tags.DELETE("/:tag_id", tag.Delete)
		}
		panels := v1.Group("/panel/")
		{
			panels.GET("/tags", panel.ListTags)
			panels.GET("/tags/:tag_id", panel.ListTagArticles)
		}
	}
}
